package judesart.app.client.ui.pub.gallery;

import judesart.app.client.controller.ArtworkController;
import judesart.app.client.controller.ArtworkControllerAsync;
import judesart.app.client.ga.Tracker;
import judesart.app.client.widget.TopInfo;
import judesart.app.domain.Artwork;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Image;

public class ForSaleDialog {

    private final ArtworkControllerAsync service = ArtworkController.Util.getInstance();

    private final Dialog dialog;
    private final Image image;
    private final TextArea message;
    private final TextField<String> msgHeader;
    private final TextField<String> emailAddress;
    private final LayoutContainer form;
    private final Html interest;
    private final Html htmlTitle;
    private final Html htmlType;
    private final Html htmlYear;
    private final Html htmlSize;
    private final Html htmlTechnic;
    private final Html htmlPrice;
    private Artwork artwork;
    private final Html thankYou;

    private final Button sendButton;

    public ForSaleDialog() {
        dialog = new Dialog();
        dialog.setSize(860, 600);
        dialog.setScrollMode(Scroll.AUTO);
        dialog.setId("id-jaw-ForSaleDialog");
        dialog.setAutoHide(true);
        dialog.setHideOnButtonClick(true);

        LayoutContainer main = new LayoutContainer(new FlowLayout());
        main.setStyleAttribute("margin", "5px 40px 5px 40px");
        main.setStyleAttribute("font-size", "1.5em");
        main.setId("id-jaw-ForSaleDialog-main");
        dialog.add(main);

        LayoutContainer columns = new LayoutContainer();
        columns.setStyleAttribute("clear", "both");
        main.add(columns);
        LayoutContainer left = new LayoutContainer();
        left.setWidth("66%");
        left.setStyleAttribute("float", "left");
        columns.add(left);
        LayoutContainer right = new LayoutContainer();
        right.setWidth("25%");
        right.setStyleAttribute("float", "right");
        columns.add(right);
        LayoutContainer clear = new LayoutContainer();
        clear.setStyleAttribute("clear", "both");
        columns.add(clear);

        LayoutContainer imgWrp = new LayoutContainer();
        imgWrp.setId("id-jaw-ForSaleDialog-image");
        image = new Image();
        imgWrp.add(image);
        left.add(imgWrp);

        htmlTitle = new Html();
        htmlTitle.setTagName("h2");
        htmlTitle.setId("id-jaw-ForSaleDialog-title");
        right.add(htmlTitle);
        LayoutContainer workInfo = new LayoutContainer();
        workInfo.addStyleName("jaw-info-medium");
        workInfo.setId("id-jaw-ForSaleDialog-info");
        htmlType = new Html();
        htmlType.setTagName("span");
        workInfo.add(htmlType);
        Html sep = new Html("/");
        sep.setTagName("span");
        workInfo.add(sep);
        htmlYear = new Html();
        htmlYear.setTagName("span");
        workInfo.add(htmlYear);
        LayoutContainer wrap = new LayoutContainer();
        htmlTechnic = new Html();
        htmlTechnic.setTagName("div");
        wrap.add(htmlTechnic);
        htmlSize = new Html();
        htmlSize.setTagName("div");
        htmlSize.addStyleName("jaw-text-alt-color");
        wrap.add(htmlSize);
        workInfo.add(wrap);
        wrap.setStyleAttribute("margin-bottom", "1em");
        Html priceLabel = new Html("Price ");
        priceLabel.setTagName("span");
        workInfo.add(priceLabel);
        htmlPrice = new Html();
        htmlPrice.setTagName("span");
        htmlPrice.addStyleName("jaw-text-alt-style");
        workInfo.add(htmlPrice);
        right.add(workInfo);

        interest = new Html();
        interest.setHtml("If you are interested in buying this work or just want more information, please use the form below to send me a message.");
        interest.addStyleName("jaw-label");
        interest.setId("id-jaw-ForSaleDialog-interest");
        interest.setStyleAttribute("margin", "2em 0 1em 0");
        interest.setStyleAttribute("display", "block");

        main.add(interest);

        form = new LayoutContainer();
        form.setId("id-jaw-ForSaleDialog-form");
        FormLayout layout = new FormLayout();
        layout.setLabelSeparator("");
        layout.setLabelAlign(LabelAlign.TOP);
        form.setLayout(layout);

        emailAddress = new TextField<String>();
        emailAddress.setFieldLabel("your email address");
        emailAddress.setAllowBlank(false);
        form.add(emailAddress, new FormData("100%"));

        msgHeader = new TextField<String>();
        msgHeader.setAllowBlank(false);
        msgHeader.setFieldLabel("subject");
        form.add(msgHeader, new FormData("100%"));

        message = new TextArea();
        message.setWidth(600);
        message.setHeight(100);
        message.setMinLength(1);
        message.setFieldLabel("message");
        message.setAllowBlank(false);
        form.add(message, new FormData("100%"));

        sendButton = new Button("Send Message");
        sendButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                sendForm();
            }
        });
        ButtonBar buttonBar = new ButtonBar();
        buttonBar.setAlignment(HorizontalAlignment.RIGHT);
        buttonBar.add(sendButton);
        form.add(buttonBar, new FormData("100%"));

        main.add(form);

        thankYou = new Html();
        thankYou.setHtml("Thank you for your message! I will be in touch shortly.");
        thankYou.setVisible(false);
        thankYou.addStyleName("jaw-label");
        thankYou.setId("id-jaw-ForSaleDialog-interest");
        thankYou.setStyleAttribute("margin", "6em 0 1em 0");
        thankYou.setStyleAttribute("text-align", "center");
        thankYou.setStyleAttribute("width", "100%");
        thankYou.setStyleAttribute("display", "block");

        main.add(thankYou);
        dialog.getButtonBar().hide();
    }

    private void sendForm() {
        sendButton.setEnabled(false);
        service.sendMessage(emailAddress.getValue(), msgHeader.getValue(),
                message.getValue(), artwork.getId(), new AsyncCallback<Void>() {

                    @Override
                    public void onSuccess(Void result) {
                        form.hide();
                        interest.hide();
                        thankYou.show();
                        dialog.getButtonBar().show();
                        sendButton.setEnabled(true);
                        Tracker.trackGoogleAnalytics("/gallery/artwork/" + artwork.getId() +"/forsale/emailSent");
                    }

                    @Override
                    public void onFailure(Throwable caught) {
                        TopInfo.display("Error!", "Unable to send message.");
                        sendButton.setEnabled(true);
                    }
                });
    }

    public void show(Artwork artwork) {
        this.artwork = artwork;
        Long imageId = artwork.getPhotos().get(0).getImageId();
        image.setUrl("/image/" + imageId + "_400x380");

        htmlTitle.setHtml(artwork.getTitle());
        htmlType.setHtml(artwork.getType().name());
        htmlSize.setHtml(artwork.getDimensions() + "cm");
        htmlTechnic.setHtml(artwork.getTechnic());
        htmlPrice.setHtml(artwork.getPrice() + "&euro;");
        htmlYear.setHtml(Long.toString(artwork.getYear()));

        emailAddress.clear();
        msgHeader.clear();
        message.clear();

        interest.show();
        form.show();
        thankYou.hide();
        dialog.getButtonBar().hide();

        dialog.show();
        Tracker.trackGoogleAnalytics("/gallery/artwork/" + artwork.getId() +"/forsale");
    }

}
